// "Copyright [2021] <Copyright Shengkai Lin>"

#include "topo_manager.h"

namespace ai_sim {

Define_Module(TopoManager);

void TopoManager::initialize() {
    EV << "Extracting Data plane" << std::endl;
    data_plane_topo.extractByProperty("dataplane");

    EV << "Extracting workers topo" << std::endl;
    workers_topo.extractByProperty("worker");

    EV << "Extracting control plane task" << std::endl;
    control_task_topo.extractByProperty("controlplane_task");

    EV << "Extracting control plane sdn" << std::endl;
    control_sdn_topo.extractByProperty("controlplane_sdn");

    cModule * dispatcher_mod = getModuleByPath("^.dispatcher");
    if (dispatcher_mod != nullptr) {
        Topology::Node *dispatcher_node =
            control_task_topo.getNodeFor(dispatcher_mod);
        control_task_topo.calculateWBSSPathsTo(dispatcher_node);
    }

    cModule * sdn_mod = getModuleByPath("^.network_controller.network_routing");
    if (sdn_mod != nullptr) {
        Topology::Node *_sdnnode =
            control_sdn_topo.getNodeFor(sdn_mod);
        control_sdn_topo.calculateWBSSPathsTo(_sdnnode);
    }
    EV_INFO << "INIT TopoManager FINISHED" << endl;
}

Topology * TopoManager::getDataplaneTopo() {
    return &data_plane_topo;
}
Topology * TopoManager::getWorkersTopo() {
    return &workers_topo;
}
Topology * TopoManager::getControlTaskTopo() {
    return &control_task_topo;
}

Topology * TopoManager::getControlSdnTopo() {
    return &control_sdn_topo;
}
Topology::Node* TopoManager::getNodeinTopo(
    Topology::Node* src_node, Topology* dest_topo) {
    omnetpp::cModule* target = src_node->getModule();
    return dest_topo->getNodeFor(target);
}

}  // namespace ai_sim
